 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include"handler.h"
#include "HandlerThread.h"
#include <unistd.h>
#include <utils/Looper.h>

using namespace android;

class Fujica_Handler;

typedef  void (*handler_callback)(int,int,int,const char *);

typedef struct handler_{
  sp<Fujica_Handler> sp_handler;
  sp<HandlerThread> mHandlerThread;
  sp<Looper> threadLooper;
}handler_t;

class Fujica_Handler : public MessageHandler
   {
	private:
    	handler_callback cb;
    public:
        virtual void handleMessage(const Message &message);
        void set_cb(handler_callback cb){this->cb=cb;}
};

void Fujica_Handler::handleMessage(const Message &message)
{

	if(this->cb){
		this->cb(message.what,message.arg1,message.arg2,message.data);
	}
}

void send_EmptyMessage_Delay(void *handle,int what,int arg1,int arg2,long uptimeMillis)
{
    if(handle)
    {
       	handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
        handler->threadLooper->sendMessageDelayed(ms2ns(uptimeMillis), handler->sp_handler, Message(what,arg1,arg2));
    }
}

void send_EmptyMessage(void *handle,int what,int arg1,int arg2)
{
    if(handle)
    {
		 handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
            handler->threadLooper->sendMessage(handler->sp_handler, Message(what,arg1,arg2));
		
    }
}

void send_Message_Delay(void *handle,int what,int arg1,char *p,long uptimeMillis){

   if(handle)
    {
		 handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
        handler->threadLooper->sendMessageDelayed(ms2ns(uptimeMillis),handler->sp_handler, Message(what,arg1,p));
    }
	
}

void send_Message(void *handle,int what,int arg1,char *p){
	
	if(handle)
    {
		 handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
        handler->threadLooper->sendMessage(handler->sp_handler, Message(what,arg1,p));
    }
}

void remove_Messages(void *handle,int what)
{
    if(handle)
    {
         handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
          handler->threadLooper->removeMessages(handler->sp_handler,what);
    }
}
void remove_ALL_Messages(void *handle)
{
    if(handle)
    {
        handler_t *handler=(handler_t *)handle;
        if(handler->threadLooper!=NULL)
             handler->threadLooper->removeMessages(handler->sp_handler);
    }
}


int  handler_init(void **handle,void (*cb)(int,int,int,const char *))
{
	if(*handle==NULL){
	
	  handler_t *handler=(handler_t *)malloc(sizeof(handler_t));
	  if(handler==NULL){
		return -1;
	  }
	  memset(handler,0,sizeof(handler_t));
	  handler->sp_handler=new Fujica_Handler();
	  handler->mHandlerThread = new HandlerThread();
      handler->mHandlerThread->start("VMS.NATIVE_LOOP");
	  handler->threadLooper =handler->mHandlerThread->getLooper();
      handler->sp_handler->set_cb((handler_callback)cb);
	  *handle=handler;
	}
    return 1;
}

int handler_deinit(void **handle)
{
    if(*handle!=NULL)
    {
		handler_t *handler=(handler_t *)handle;
        handler->mHandlerThread->quit();
		free(handler);
		*handle=NULL;
        return true;
    }
    return false;

}


